/**
 * @Description: 其余公共提问器
 * @author Beon
 * @date 2021/1/25
*/
import * as inquirer from 'inquirer';
import {OraLoading} from '../decorators/oraLoading';
import {LzgitCliFile} from '../utils/lzgitCliFile';
import {ErrorCatch} from '../decorators/errorCatch';
import {QuestionCollection} from 'inquirer';

export class OtherInquirer{
    @OraLoading.beProxy
    static status: OraLoading;
    private static checkboxInquirer = {
        type: 'checkbox',
        name: 'checkbox',
        message: '选择需要推送的远程库',
        choices: []
    }
    private static configInquirer = {
        type: 'editor',
        message: '请输入配置信息：',
        name: 'editor',
        default: ''
    }

    /**
     * origin选择提问器
     * @param origin
     * @returns {Promise<>}
     */
    static async checkboxInquirerFn(origin: string[]): Promise<string[]> {
        this.dealCheckboxChoices(origin);

        this.status.oraLoadingStop();

        const getInquirer = await inquirer.prompt([this.checkboxInquirer]) as {checkbox: string[]};

        this.status.oraLoadingStart();

        return getInquirer.checkbox;
    }

    /**
     * 处理origin选择数组
     * @param origin
     */
    static dealCheckboxChoices(origin: string[]): void {
        this.checkboxInquirer.choices = origin.map((item: string) => ({
            name: item,
            value: item
        }));
    }

    /**
     * 修改配置文件
     * @returns {Promise<void>}
     */
    @ErrorCatch.errorExit('请检查输入的json对象是否格式规范！')
    static async wirteConfig(): Promise<void> {
        this.configInquirer.default = JSON.stringify(await LzgitCliFile.getFileData());

        this.status.oraLoadingStop();

        const getInquirer = await inquirer.prompt([this.configInquirer]) as {editor: string};

        this.status.oraLoadingStart();

        const json = JSON.parse(getInquirer.editor);

        LzgitCliFile.wirteFile(json);
    }

    /**
     * 基础提问器
     * @param config
     * @returns {Promise<any>}
     */
    static async baseInquirer<T>(config: QuestionCollection<T>): Promise<T> {
        this.status.oraLoadingStop();

        const getInquirer = await inquirer.prompt(config);

        this.status.oraLoadingStart();

        return getInquirer;
    }
}
